<?

class hotel_instances_mcs {




// FUNCTION // lookup hotel based on Micros Travel instance values. Return hotel values if true.
function lookup_hotel($params=''){


	$params = in($params);

	if(!empty($params['supplier_code'])){
	
		$sql = "SELECT T.tree_id, T.name, T.description, T.type_id, T.lineage_base, LENGTH( T.lineage_base ) - LENGTH( REPLACE( T.lineage_base,  ',',  '' ) ) as depth, SUBSTRING_INDEX(T.lineage_base, ',', -1) AS parent_id, CONCAT(T.lineage_base,',',T.tree_id) AS lineage, T.main_photo, I.instance_id, I.tour_op, I.hotel_name, I.destination_airport, I.resort, I.resort_code, I.holiday_code, I.hotel_code
		FROM tree AS T
		INNER JOIN hotel_instances_mcs AS I on T.tree_id = I.tree_id
		WHERE I.instance_string = '".$params['hotel_code'].".".$params['holiday_code'].".".$params['resort_code'].".".$params['resort'].".".$params['destination_airport'].".".$params['hotel_name'].".".$params['tour_op']."'
		LIMIT 1	";	

		return apply_query($sql, $num_rows, $params['cache']);
			
	}
	
}





// FUNCTION // search for hotel instances
function search_hotel_instances($params=''){


	$params = in($params);

	if($params['tree_id']) $where .= " AND tree_id = '".$params['tree_id']."' ";

if(isset($where)){

    $sql = "SELECT instance_id, tree_id, instance_string, tour_op, hotel_name, destination_airport, resort, 	resort_code, holiday_code, hotel_code, date_linked
    FROM hotel_instances_mcs
    WHERE 1 = 1 
	".$where."
    ORDER by date_linked"; 

	return apply_query($sql, $num_rows, $params['cache']);
	
}

}




// FUNCTION // number of all accommodation
function number_flagged_instances(){


    $sql = "SELECT COUNT( instance_id ) AS count, instance_date
	FROM hotel_instances_mcs_flagged
	ORDER BY instance_date"; 
 
	return apply_query($sql);

}



// FUNCTION // get all accommodation
function search_flagged_instances($params=''){


	$params = in($params);

	if($params['prefix']) $where .= " AND UPPER(LEFT(hotel_name, 1)) = '".$params['prefix']."' ";
	if($params['hotel_name']) $where .= " AND hotel_name LIKE '%".$params['hotel_name']."%' ";
	if($params['resort']) $where .= " AND resort LIKE '%".$params['resort']."%' ";
	if($params['tour_op']) $where .= " AND tour_op = '".$params['tour_op']."' ";
	if($params['airport_code']) $where .= " AND destination_airport = '".$params['airport_code']."' ";
	
	if($params['order'] == ("DESC"||"ASC")) {
		$sql_order = $params['order'];
	}else{
		$sql_order = "DESC";
	}
	
	if(!empty($params['sort'])) {
		$sql_sort = " ORDER by ".$params['sort']." ".$sql_order." ";
	}else{
		$sql_sort = " ORDER by instance_date ".$sql_order." ";
	}


    $sql = "SELECT instance_id, tour_op, hotel_name, destination_airport, resort, resort_code, holiday_code, hotel_code, instance_date
    FROM hotel_instances_mcs_flagged
    WHERE 1 = 1 
	".$where."
    ".$sql_sort." "; 

	

	return apply_query($sql);

}



// FUNCTION // get a single instance from the table
function get_flagged_instance($params){


	if(!empty($params)){

		$params = in($params);	
		
		$sql = "SELECT instance_id, tour_op, hotel_name, destination_airport, resort, resort_code, holiday_code, hotel_code, instance_date
		FROM hotel_instances_mcs_flagged
		WHERE instance_id = '".$params['instance_id']."'
		LIMIT 1	";
				
		return apply_query($sql);
		
	}
	
}




// FUNCTION // search to see if a record has been entered in the table
function lookup_flagged_instance($params){


	$params = in($params);

    $sql = "SELECT instance_id, tour_op, hotel_name, destination_airport, resort, resort_code, holiday_code, hotel_code, instance_date
    FROM hotel_instances_mcs_flagged
    WHERE tour_op = '".$params['tour_op']."' 
	AND hotel_name = '".$params['hotel_name']."'
	AND destination_airport = '".$params['destination_airport']."'
	AND resort = '".$params['resort']."'
	AND resort_code = '".$params['resort_code']."'
	AND holiday_code = '".$params['holiday_code']."'
	AND hotel_code = '".$params['hotel_code']."'
	LIMIT 1	";	
	
	return apply_query($sql);
	
}


// FUNCTION // search to see if a record has been entered in the table
function flag_not_found($params=''){

	if(!$this->lookup_flagged_instance($params)){

		$params = in($params);
		
		$sql = "INSERT INTO hotel_instances_mcs_flagged
		SET instance_string = CONCAT('".$params['hotel_code'].".".$params['holiday_code'].".".$params['resort_code'].".".$params['resort'].".".$params['destination_airport'].".".$params['hotel_name'].".".$params['tour_op']."'),
		tour_op = '".$params['tour_op']."', 
		hotel_name = '".$params['hotel_name']."',
		destination_airport = '".$params['destination_airport']."',
		resort = '".$params['resort']."',
		resort_code = '".$params['resort_code']."',
		holiday_code = '".$params['holiday_code']."',
		hotel_code = '".$params['hotel_code']."',
		instance_date = '".mktime()."' ";
	
		$result = mysql_query($sql) or die (mysql_error());	

	}

}





// FUNCTION // Insert a linked instance into the instance table
function link_instance($params=''){

	$instance_values = array();
	
	$ins_arg['instance_id'] = $params['instance_id'];
	$instance_values = $this->get_flagged_instance($ins_arg);
	$instance_values = $instance_values[0];
	$instance_values['tree_id'] = $params['tree_id'];

	$this->insert_linked_instance($instance_values);

	$this->deleted_flagged_instance($params['instance_id']);
	
}





// FUNCTION // search to see if a record has been entered in the table
function insert_linked_instance($params=''){

if(!empty($params)){


	$params = in($params);

	$sql = "INSERT INTO hotel_instances_mcs
	SET instance_string = CONCAT('".$params['hotel_code'].".".$params['holiday_code'].".".$params['resort_code'].".".$params['resort'].".".$params['destination_airport'].".".$params['hotel_name'].".".$params['tour_op']."'),
	tree_id = '".$params['tree_id']."',
	tour_op = '".$params['tour_op']."', 
	hotel_name = '".$params['hotel_name']."',
	destination_airport = '".$params['destination_airport']."',
	resort = '".$params['resort']."',
	resort_code = '".$params['resort_code']."',
	holiday_code = '".$params['holiday_code']."',
	hotel_code = '".$params['hotel_code']."',
	date_linked = '".mktime()."' ";

	$result = mysql_query($sql) or die (mysql_error());	
	
}


}


// FUNCTION // Delete a flagged instance
function deleted_flagged_instance($instance_id=''){

	if(!empty($instance_id)){
		
		$sql = "DELETE FROM hotel_instances_mcs_flagged
		WHERE instance_id = '".$instance_id."' ";
		
		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}


// FUNCTION // Delete a linked instance
function deleted_linked_instance($params){

	if(!empty($params['instance_id'])){
		
		$sql = "DELETE FROM hotel_instances_mcs
		WHERE instance_id = '".$params['instance_id']."' ";

		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}






// Function delete repeated instances - these should not occur to start with!
function delete_repeats(){

	$sql = "SELECT
CONCAT(`tour_op`,`hotel_name`,`destination_airport`,`resort`,`resort_code`,`holiday_code`,`hotel_code`) AS string, instance_id
	FROM `hotel_instances_mcs_flagged`
	ORDER by string";



	$result = mysql_query($sql) or die (mysql_error());
	$num_rows = mysql_num_rows($result);

	if($num_rows){
		$values_array = array();
		while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
			$values_array[] = array_map('array_stripslashes', $row);
			
		}
		
		$i = 0;
		// set for first time
		$check_string = $values_array[0]['string'];
		foreach($values_array as $my_row){
		
			$i++;
			$j = $i - 1;
			$old_check_string = $values_array[$j]['string'];
			

			// if new is different to old then delete
			if($check_string==$old_check_string){
				$this->deleted_flagged_instance($my_row['instance_id']);
				echo "delete ".$my_row['instance_id'];
			}else{
				$check_string = $my_row['string'];
			}
			
		}
		
	}



}

// FUNCTION // Update a photo
function move_update_mcs_instances($params){

	if(!empty($params)){
        
		$params = array_map('array_trim', $params);
	
		$sql = "UPDATE hotel_instances_mcs
		SET tree_id = '".$params['move_to_tree_id']."'
		WHERE tree_id = '".$params['delete_tree_id']."' "; 
			
		$result = mysql_query($sql) or die(mysql_error());
	
	}

}


// FUNCTION // Delete all instances attached to a tree ID
function delete_instances_by_tree($tree_id){

	if(!empty($tree_id)){
		
		$sql = "DELETE FROM hotel_instances_mcs
		WHERE tree_id = '".$tree_id."' ";

		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}


// FUNCTION // Delete instances that are older then a given date
function delete_old_instances($date){

	if(!empty($date)){
		
		$sql = "DELETE FROM hotel_instances_mcs_flagged
		WHERE instance_date < '".$date."' ";

		$result = mysql_query($sql) or die (mysql_error());	
	
	}

}



}

?>